package com.example.yls.http_get_post2;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class MainActivity extends AppCompatActivity {
    private static final String TAG="MainActivity";
    public static final int GET_IMG =1001 ;
    private static final int GET_PHONE =1002 ;
    private Button btn1;
    private Button btn2;
    private ImageView iv_img;
    private Handler handler;
    private EditText edt_phone;
    private TextView txt_result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        iv_img=findViewById(R.id.iv_img);
        btn1=findViewById(R.id.btn1);
        btn2=findViewById(R.id.btn2);
        edt_phone=findViewById(R.id.edt_phone);
        txt_result=findViewById(R.id.txt_result);

        handler=new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message message) {
                if(message.what==GET_IMG){
                    Bitmap bitmap=(Bitmap)message.obj;
                    iv_img.setImageBitmap(bitmap);
                    return true;
                }else if(message.what==GET_PHONE){
                    String result= (String) message.obj;
                    txt_result.setText(result);
                    return true;
                }
                return false;
            }
        });

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        //useHttpUrlConnetionGET();
                        String phone=edt_phone.getText().toString();
                        useHttpUrlConnetionGET(phone);
                    }
                }).start();
            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String phone=edt_phone.getText().toString();
                        useHttpUrlConnetionPOST(phone);
                    }
                }).start();
            }
        });

    }

    private void useHttpUrlConnetionPOST(String phone) {
        String baseUrl="http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo";
        //String phoneUrl=baseUrl.concat("?mobileCode=").concat(phone).concat("&userID=");

        try {
            URL url=new URL(baseUrl);
            HttpURLConnection httpURLConnection= (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoInput(true);
            httpURLConnection.setDoOutput(true);

            String body="mobileCode=".concat(phone).concat("&userID=");
            OutputStream outputStream=httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(outputStream,
                    "utf-8"));
            bufferedWriter.write(body);
            bufferedWriter.flush();
            bufferedWriter.close();

            int code=httpURLConnection.getResponseCode();
            Log.e(TAG,"code="+code);

            InputStream inputStream=httpURLConnection.getInputStream();

            /*Bitmap bitmap= BitmapFactory.decodeStream(inputStream);

            Message message=Message.obtain();
            message.what=GET_IMG;
            message.obj=bitmap;
            handler.sendMessage(message);*/

            //iv_img.setImageBitmap(bitmap);

            BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream,"utf-8"));

            StringBuffer stringBuffer=new StringBuffer();
            String line=bufferedReader.readLine();
            while(line!=null){
                stringBuffer.append(line);
                line=bufferedReader.readLine();
            }
            Log.e(TAG,"stringBuffer="+stringBuffer.toString());

            Message message=Message.obtain();
            message.what=GET_PHONE;
            message.obj=stringBuffer.toString();
            handler.sendMessage(message);

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //private void useHttpUrlConnetionGET() {
    private void useHttpUrlConnetionGET(String phone) {
        String strUrl="http://www.baidu.com";
        String imgUrl="http://ica.gdcp.cn/service?wdApplication=nr&wdService=getData&wdtest=false&path=/upload/image/20190228/1551315546439054314/wps846C.tmp.png";
        String imgUrl2="http://ica.gdcp.cn/service?wdApplication=nr&wdService=getData&wdtest=false&path=/upload/image/20190118/1547824760637005178/wpsA302.tmp.png";

        String baseUrl="http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo";
        String phoneUrl=baseUrl.concat("?mobileCode=").concat(phone).concat("&userID=");

        try {
            //URL url=new URL(strUrl);
            //URL url=new URL(imgUrl);
            //URL url=new URL(imgUrl2);
            URL url=new URL(phoneUrl);

            HttpURLConnection httpURLConnection= (HttpURLConnection) url.openConnection();
            int code=httpURLConnection.getResponseCode();
            Log.e(TAG,"code="+code);

            InputStream inputStream=httpURLConnection.getInputStream();

            /*Bitmap bitmap= BitmapFactory.decodeStream(inputStream);

            Message message=Message.obtain();
            message.what=GET_IMG;
            message.obj=bitmap;
            handler.sendMessage(message);*/

            //iv_img.setImageBitmap(bitmap);

            BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream,"utf-8"));

            StringBuffer stringBuffer=new StringBuffer();
            String line=bufferedReader.readLine();
            while(line!=null){
                stringBuffer.append(line);
                line=bufferedReader.readLine();
            }
            Log.e(TAG,"stringBuffer="+stringBuffer.toString());

            Message message=Message.obtain();
            message.what=GET_PHONE;
            message.obj=stringBuffer.toString();
            handler.sendMessage(message);

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
